# -*- coding: UTF-8 -*-
import sys
import yaml
import os

"""
value, 字段名
path, 域名或IP：port
user, 登录用户
pwd, 登录密码
db, 数据库名
db_host, 数据库地址
db_port, 数据库端口
db_user, 数据库用户
db_pwd 数据库密码
示例：
*,*,*/=/*,*/=/...........
"""
write_data = {}
try:
    if sys.argv[1]:
        sys_value = sys.argv[1].replace("\n", "").split("/=/")
        name_list = ["value", "path", "user", "pwd", "db", "db_user", "db_pwd", "db_host", "db_port"]
        for sys in sys_value:
            new_sys = sys.split(",")
            cunt = len(new_sys)
            write_data.update({new_sys[0]: {}})
            for c in range(1, cunt):
                try:
                    if new_sys[c] == "":
                        data = "#"
                    else:
                        data = new_sys[c]
                    values = {
                        name_list[c]: data
                    }
                    write_data.get(new_sys[0]).update(values)
                except Exception as e:
                    pass
                values = {}
except IndexError:
    pass
except Exception as e:
    pass

PAR_CONFIG = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'config', 'params_config.yaml')


def write(data_yaml, is_clear=False, path=PAR_CONFIG):
    """
    参数写入yaml文件
    """
    if data_yaml:
        # 写入到yaml文件
        wte = "a+"
        if is_clear:
            wte = "w"
        with open(path, wte, encoding="utf-8") as f:
            yaml.dump(data_yaml, f)
    else:
        pass


if __name__ == '__main__':
    write(data_yaml=write_data, is_clear=True)
